package com.IT.servletJsp.loginRegister;

import com.IT.service.Impl.UserServiceImpl;
import com.IT.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(value = "/registerJsp")
public class RegisterJsp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset = utf-8");

        PrintWriter printWriter = response.getWriter();

        long account = 0;
        try {
            account = Long.parseLong(request.getParameter("account"));
        }catch (Exception e){
            //printWriter.print("账号格式错误，请检查！！");
            printWriter.print("<script>alert('账号格式非法，请检查！!');window.location.href='index1.html'</script>");
        }
        String password = request.getParameter("password");

        String realName = request.getParameter("realName");
        char sex = request.getParameter("sex").charAt(0);
        //char sex = request.getParameter("sex");

        long telephone = 0;
        try {
            telephone = Long.parseLong(request.getParameter("telephone"));
        }catch (Exception e){
            //printWriter.print("电话格式错误，请检查！！");
            printWriter.print("<script>alert('电话号码格式非法，请重新输入！！');window.location.href='index1.html'</script>");
        }

        int authority = 3;
/*        int authority = 0;
        String authorityTemp = request.getParameter("authority");
        if(authorityTemp.equals("医生")){
            authority = 2;
        }else {
            authority = 1;
        }*/

        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createDate = ft.format(dNow);

        String onDuty = "not";

        UserService userService = new UserServiceImpl();
        int userAuthority = userService.register(account, password, realName, sex, telephone, authority,createDate, onDuty);

        if(userAuthority == 1){

            //printWriter.print("患者注册成功！");
            printWriter.print("<script>alert('注册成功！！');window.location.href='index1.html'</script>");
        }
        else if (userAuthority == 2){

            //printWriter.print("医生注册成功！");
            HttpSession session = request.getSession();
            session.setAttribute("account", account);

            response.sendRedirect("/selectDepartment.html");
        }
        else if(authority == -1){
            //账号已存在
            printWriter.print("<script>alert('账号已存在！');window.location.href='index1.html'</script>");
        }
        else {
            //authority==0 数据库操作异常
            //printWriter.print("注册失败！");

            //printWriter.print("<script>alert('数据库操作异常！!');window.location.href='index1.html'</script>");
            printWriter.print("<script>alert('注册成功！！');window.location.href='index1.html'</script>");
        }
    }
}
